home *** CD-ROM | disk | FTP | other *** search
/ Univers Interactif 3 / INTERACTIF.BIN / pc / planeten / internet / machttp2.sit / MacHTTP 2.0 / MacHTTP Software & Docs / Tutorials / Extending MacHTTP Scripts / Script5.txt < prev    next >
Text File  |  1994-12-11  |  6KB  |  128 lines

  1. -- set these properties outside of the event.  That way they will only be set 
  2. -- once each time the app is run, not once for each event.
  3. property crlf : (ASCII character 13) & (ASCII character 10)
  4. -- This is a standard header for HTML files.
  5. property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & ┬
  6.     "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
  7. -- Idletime is how long you want it to remain open to 
  8. -- wait for another event. Idletime is in seconds.
  9. -- Datestamp will contain the current date.  Initialize it here.
  10. property idletime : 300 -- set to 5 minutes
  11. property datestamp : 0
  12.  
  13. -- this bit of code outside the sdoc event is executed at launch-time
  14. -- it is neccesary because an idle event can happen between
  15. -- launch and the receipt of an sdoc event (and in fact often does)
  16. set datestamp to current date
  17.  
  18. -- This is the loop for AppleEvents received by the application.
  19. -- When you check the syntax, AppleScript will ask you to locate 
  20. -- MacHTTP and will set the correct name at that time.
  21. on ╟event WWW╜sdoc╚ path_args ┬
  22.     given ╟class kfor╚:http_search_args, ╟class post╚:post_args, ╟class meth╚:method, ╟class addr╚:client_address, ╟class user╚:username, ╟class pass╚:password, ╟class frmu╚:from_user, ╟class svnm╚:server_name, ╟class svpt╚:server_port, ╟class scnm╚:script_name, ╟class ctyp╚:content_type
  23.     -- Variables available for use:
  24.     -- http_search_args - stuff in the URL after a ?
  25.     -- post_args - stuff in the URL after a $
  26.     -- method - GET, POST, etc. Used to tell if post_args are valid
  27.     -- client_address - IP address or domain name of remote client's host
  28.     -- from_user - non-standard. e-mail address of remote user
  29.     -- username - authenticated user name
  30.     -- password - authenticated password
  31.     -- server_name - name or IP address of this server
  32.     -- server_port - TCP/IP port number being used by this server
  33.     -- script_name - URL name of this script
  34.     -- content_type - MIME content type of post_args
  35.     
  36.     -- Using the "try" clause causes the "on error" routine to be run
  37.     -- if an error occurs instead of crashing.
  38.     -- If the error occurs outside the "try"/"end try" space then
  39.     -- the "on error" routine is NOT run.
  40.     try
  41.         -- save the current date and time to check later for quitting
  42.         set datestamp to current date
  43.         
  44.         -- I don't return all of the parameters because most are uninteresting or 
  45.         -- are not supported by all clients.
  46.         -- In addition, we only parse the post_args, since this is the only 
  47.         -- data entered by the user (all else supplied by MacHTTP).
  48.         set return_page to http_10_header ┬
  49.             & ┬
  50.             "<HTML><HEAD><TITLE>Parsed Results</TITLE></HEAD>" & "<BODY><H1>Parsed Results</H1>" & return ┬
  51.             & "<H4>http_search_args</H4>" & return ┬
  52.             & http_search_args & return
  53.         set return_page to return_page & "<H4>post_args</H4><PRE>" & return
  54.         
  55.         -- this creates a list of "name=value" pairs
  56.         set postarglist to tokenize post_args with delimiters {"&"}
  57.         
  58.         -- The section below converts the list to readable output, putting each item onto a separate line.
  59.         -- Uses DecodeURL to convert %XX encodings and Tokenize/JoinList to
  60.         -- convert "+" to spaces.
  61.         
  62.         -- store the original AppleScript text item delimiters
  63.         set oldDelim to AppleScript's text item delimiters
  64.         -- use "=" to delimit the pairs
  65.         set AppleScript's text item delimiters to {"="}
  66.         -- initialize postargtext in case the item is empty
  67.         set postargtext to ""
  68.         -- traverse the list item by item
  69.         repeat with currpostarg in postarglist
  70.             set postargtext to postargtext & ┬
  71.                 (Decode URL (DePlus (last text item of currpostarg))) & return & return
  72.         end repeat
  73.         -- restore the old AppleScript text item delimiters settings
  74.         set AppleScript's text item delimiters to oldDelim
  75.         
  76.         set return_page to return_page ┬
  77.             & postargtext & "</PRE>" & return ┬
  78.             & "<H4>client_address</H4>" & return ┬
  79.             & client_address & return
  80.         set return_page to return_page ┬
  81.             & "<HR><I>Results generated at: " & (current date) ┬
  82.             & "</I>" & "</BODY></HTML>"
  83.         -- return the page created.  A return statement ends the 
  84.         -- processing of the AppleEvent
  85.         return return_page
  86.         
  87.         -- here is the routine to run if an error occurs
  88.         -- errMsg contains the message sent by the System
  89.         -- errNum contains the number of the error (negative for System, AE, or AS errors)
  90.     on error errMsg number errNum
  91.         -- create a page of HTML text to return
  92.         set return_page to http_10_header ┬
  93.             & "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" ┬
  94.             & "<BODY><H1>Error Encountered!</H1>" & return ┬
  95.             & "An error was encountered while trying to run this script." & return
  96.         set return_page to return_page ┬
  97.             & "<H3>Error Message</H3>" & return & errMsg & return ┬
  98.             & "<H3>Error Number</H3>" & return & errNum & return ┬
  99.             & "<H3>Date</H3>" & return & (current date) & return
  100.         set return_page to return_page ┬
  101.             & "<HR>Please notify Jon Wiederspan at " ┬
  102.             & "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" ┬
  103.             & " of this error." & "</BODY></HTML>"
  104.         -- return the page created.  A return statement ends the 
  105.         -- processing of the AppleEvent
  106.         return return_page
  107.     end try
  108. end ╟event WWW╜sdoc╚
  109.  
  110. -- The idle function is run everytime the system sends an idle message.
  111. -- If the current date is more than idletime seconds more than the last date 
  112. -- then it is time to quit.
  113. -- The return value tells the system how long to wait before
  114. on idle
  115.     if (current date) > (datestamp + idletime) then
  116.         quit
  117.     end if
  118.     return 5
  119. end idle
  120.  
  121. -- This code allows you to do any clean-up that might be necessary.
  122. -- Example: you could write the quit event time to a log to see 
  123. -- how often the applet is running.
  124. on quit
  125.     -- do any clean-up chores here
  126.     continue quit
  127. end quit
  128.